package com.softcomputer.rpt;

import com.softcomputer.rpt.ejb.ExchangeSession;
import com.softcomputer.rpt.entity.ExCourse;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ManagedBean(name = "course")
@SessionScoped
public class CoursesManagementBean implements Serializable {

   @EJB
   private ExchangeSession exCourseSession;
   private ExCourse course;
   private List<BigDecimal> values;
   private List<String> currencies;
   private String currency;
   private static final Log LOG = LogFactory.getLog(OperationManagementBean.class);

   public CoursesManagementBean() {
   }

   public List<BigDecimal> getValues() {

      LOG.info("inDollar values requested...");
      try {
         values = exCourseSession.getDollarRate();
      } catch (Exception exception) {
         LOG.error("Unable to read inDollar values: " + exception.getMessage());
         return new ArrayList<BigDecimal>();
      }

      LOG.info("idDollar values successfully recieved");
      return values;
   }

   public List<ExCourse> getExchangeCourses() {

      List<ExCourse> exchangeCoursesList;

      try {
         exchangeCoursesList = exCourseSession.retrieve();
      } catch (Exception exception) {
         LOG.error("Unable to retrieve exchange rates: " + exception.getMessage());
         return new ArrayList<ExCourse>();
      }

      LOG.info("Exchange rates list retrieved successfully");
      return exchangeCoursesList;
   }

   public List<String> getCurrencies() {

      LOG.info("inDollar values requested...");

      try {
         currencies = exCourseSession.getCurrency();
      } catch (Exception exception) {
         LOG.error("Unable to read currencies: " + exception.getMessage());
         return new ArrayList<String>();
      }

      LOG.info("Curreicues successfully recieved");
      return currencies;
   }

   public ExCourse getCourse() {
      if (course == null) {
         course = new ExCourse();
      }
      return course;
   }

   public void setCurrency(String curr) {
      currency = curr;
   }

   public String getCurrency() {
      return currency;
   }
}
